package com.xian.im.modules.command.cmd;

import com.xian.feign.UserChannelClient;
import com.xian.im.common.constant.CommandConstant;
import com.xian.im.common.model.session.SessionUser;
import com.xian.im.common.proto.MsgRequest;
import com.xian.im.common.proto.MsgResponse;
import com.xian.im.common.utils.sys.SpringContextHolder;
import com.xian.im.modules.command.Command;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @program: xian-im-server
 * @description:
 * @author: liru.xian
 * @create: 2021-01-27 09:26
 */
@Slf4j
public class HeartbeatHandlerCommand implements Command {

  /**
   * 执行
   *
   * @param request
   * @param ctxId
   */
  @Override
  public void exec(MsgRequest request, String ctxId) {
    UserChannelClient userChannelClient = SpringContextHolder.getBean(UserChannelClient.class);

    Integer cmd = request.getCmd();

    if (CommandConstant.HEART_BEAT.equals(cmd)) {
      SessionUser sessionUser = userChannelClient.session(ctxId);
      log.debug("心跳  {},{}", request, sessionUser);
      MsgResponse response = MsgResponse.buildSuccess();
      BeanUtils.copyProperties(request, response);
      userChannelClient.bindSession(sessionUser, ctxId);
      userChannelClient.pushChannelMessage(ctxId,response);
    }
  }

  /**
   * 获取命令
   *
   * @return
   */
  @Override
  public Integer getCmd() {
    return CommandConstant.HEART_BEAT;
  }
}
